type useModel = (options: Options) => ModelRetrun;
import { createApp, ref } from "vue";
import Model from "./Model.vue";
export interface Options {
  titel: string;
  context?: string;
  custom?: string;
  okText?: string;
  cancelText?: string;
  blank?: string;
  hiddenCancel?: boolean;
  onOk?: () => boolean | undefined;
  onCancel?: () => boolean | undefined;
}

interface ModelRetrun {
  showMessage: () => void;
  onBack: () => void;
  cancelBack: () => void;
}

const useModel: useModel = (options: Options): ModelRetrun => {
  const status = ref<boolean>(false);
  const { onOk, onCancel } = options;
  const changStatus = (state: boolean) => {
    status.value = state;
  };
  const onBack = () => {
    if (onOk && !onOk()) {
      return;
    }
    changStatus(false);
  };
  const cancelBack = () => {
    if (onCancel && !onCancel()) {
      return;
    }
    changStatus(false);
  };
  const showMessage = () => {
    status.value = true;
  };
  const messageNode = document.createElement("div");
  document.body.appendChild(messageNode);
  createApp(Model, {
    status,
    options,
    changStatus,
    onBack,
    cancelBack,
  }).mount(messageNode);
  return {
    showMessage,
    onBack,
    cancelBack,
  };
};

export { useModel };
